package com.sunday.common.netty.e7_Websocket.client;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler;

public class WebsocketClientChannel extends ChannelInitializer {

    private WebSocketClientHandler webSocketClientHandler;

    public WebsocketClientChannel(WebSocketClientHandler webSocketClientHandler) {
        this.webSocketClientHandler = webSocketClientHandler;
    }

    @Override
    protected void initChannel(Channel ch) throws Exception {

        ch.pipeline()
                // HttpClientCodec: HTTP 客户端编解码器，支持 HTTP 请求/响应的编码和解码。
                .addLast(new HttpClientCodec())
                // HttpObjectAggregator: 将多个消息转换成单一的消息对象。
                .addLast(new HttpObjectAggregator(8192))
                // WebSocketClientCompressionHandler: 压缩 WebSocket 帧。
                .addLast(WebSocketClientCompressionHandler.INSTANCE)
                // WebSocketClientHandler: 处理 WebSocket 协议的数据帧。
                .addLast(webSocketClientHandler);
    }

}
